Here we see Decision Tree Regression with Python Implementation.
Import Libraries and set up directories and read the data set.
import os import pandas as pd import numpy as np from sklearn import tree from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score
os.chdir(“/Users/mac/Downloads/”)
T2 = pd.read_excel(“Latest Covid-19 India Status.xlsx”)
Here we have 7 variables, so we need to mention like 0 to 6.
simple random sampling as we use numerical data.
train, test = train_test_split(T2, test_size=0.2)
And we build a Regression Model like below. We give max_depth=2, if the data is more, then tree will start growing high. If we give max_depth = 2, then maximum node for a particular leaf should be 2, not more than that.
Model = tree.DecisionTreeRegressor(max_depth=2).fit(train.iloc[:,0:6],train.iloc[:,6])
We train Model using training data as above.
We predict, test data target values and we compare the predicted values with the actual values.
predictions = Model.predict(test.iloc[:,0:6])
With this we will end up this page, The next would be Error Metrics.